(function() {
	var editckeditor;
	Path.on("/main/__basename__.html", function(_current_location) {
		var qs = _current_location.query
			// 拉取需要修改的文本内容
		var _id = qs.get("_id");
		console.log(_id);

		// Promise创建编辑器
		new Promise(function(resolve, reject) {
			require(["ckeditor"], function() {
				if (!editckeditor) {
					editckeditor = CKEDITOR.replace("ckeditor");
				};
				resolve(editckeditor);
			});
		}).then(function(editor) {
			// 拉取文章信息
			coAjax.get(public_api.news.get.detail_by_id + _id, function(result) {
				App.set("$Cache.main.articleedit.data", result);
				console.log(result);
				// 初始化编辑器
				console.log(editor);
				console.log(result);
				try {
					editor.setData(decodeURIComponent(result.html));
				} catch (e) {
					editor.setData((result.html));
					console.log("error,", e);
				};
			}, function(e) {
				console.log("error,", e)
			});
		});
		// 拉取板块信息
		coAjax.get(public_api.section.get.list_all, function(result) {
			App.set("$Cache.main.articleedit.section_list", result);
		});
	});


	// 选择板块
	App.set("$Event.main.articleedit.modulechange", function() {
		if (this.value == "addmodule") {
			// 添加板块
			App.set("$Cahce.main.articleedit.openaddmodule", true);
		};
	});
	// 取消新增模块
	App.set("$Event.main.articleedit.canceladdmodule", function() {
		App.set("$Cahce.main.articleedit.openaddmodule", false);
		App.set("$Cache.main.articleedit.article.module", "");
	});
	// 新增板块
	App.set("$Event.main.articleedit.createmodule", function() {
		var data = App.get("$Cache.main.articleedit.newmodule");
		coAjax.post(public_api.section.create, data, function(result) {
			// console.log(result);
			App.unshift("$Cache.main.articleedit.section_list", result);
			App.set("$Cache.main.articleedit.data.section", result._id);
			alert("success", "板块添加成功");
			App.set("$Cahce.main.articleedit.openaddmodule", false);
		});
	});
	// 修改封面图片
	App.set("$Event.main.articleedit.addViewPort", function() {
		var file = this.files[0];
		if (file) {
			var upload_id_photo_dtd = new $.Deferred();
			App.set("$Cache.main.articleedit.viewport.isadding", true);
			$.when(upload_image(file, upload_id_photo_dtd)).then(function(image_url) {
				App.set("$Cache.main.articleedit.data.imgurl", image_url);
				App.set("$Cache.main.articleedit.viewport.isadding", false);
			});
		};
	});
	// 删除封面图片
	App.set("$Event.main.articleedit.removevieport", function() {
		App.set("$Cache.main.article.data.imgurl", "");
	});
	// 发布修改文章
	App.set("$Event.main.articleedit.addarticle", function() {
		var data = App.get("$Cache.main.articleedit.data") || {};
		// data.html = editckeditor.document.getBody().getHtml();
		data.html = encodeURIComponent(editckeditor.getData());
		// console.log(data);
		if (data == {}) {
			alert("warn", "请先完善文章内容");
			return;
		};
		if (!data.title) {
			alert("warn", "请先填写文章标题！");
			return;
		};
		if (!data.author) {
			alert("warn", "请先填写文章作者!");
			return;
		};
		if (data.html == "<p><br></p>") {
			alert("warn", "请填写文章内容！！");
			return;
		};
		if (data.section == "none") {
			data.section = "";
		};
		coAjax.put(public_api.news.update, data, function(result) {
			alert("success", "文章修改成功");
			Path.jump("/main/articlemanager.html");
		});
	});
}());